package view.GUI.collections;

import java.util.Map;
import javax.swing.table.AbstractTableModel;
import view.GUI.LabelReader;

/**
 *
 * @author Ben
 */
public  class TableMapModel extends AbstractTableModel implements ICollectionTableModel{
    private Map data;
    private String[] columnNames = {LabelReader.getInstance().getString("Key"),
    LabelReader.getInstance().getString("Value2")};

    public TableMapModel(Map data){
        this.data = data;
    }

    public String toString() {
        return data.toString();
    }

    public void addRow(int row){
        data.put("0","0");
    }

    public void removeRow(int row){
        Object[] keys = data.keySet().toArray();
        data.remove(keys[row]);
    }

    public int getColumnCount() {
        return columnNames.length;
    }

    public int getRowCount() {
        return data.size();
    }

    public String getColumnName(int col) {
        return columnNames[col];
    }

    public Object getValueAt(int row, int col) {
        Object[] keys = data.keySet().toArray();
        if(col == 0){
                return keys[row];
        }else{
                return data.get(keys[row]);
        }
    }

    public Class getColumnClass(int c) {
        return getValueAt(0, c).getClass();
    }

    public boolean isCellEditable(int row, int col) {
        return true;
    }

    public void setValueAt(Object value, int row, int col) {
        Object[] keys = data.keySet().toArray();
        if(col == 0){
                Object mappedValue = data.get(keys[row]);
                data.remove(keys[row]);
                data.put(value,mappedValue);
        }else if(col == 1){
                data.put(keys[row],value);
        }
        fireTableDataChanged();
    }
}
